package com.whenjun.service.core;

import com.ifootbook.util.Util;
import com.whenjun.entity.AccessLog;
import com.whenjun.mapper.AccessLogMapper;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class AccessLogService extends LoginService{

    @Autowired
    private AccessLogMapper accessLogMapper;

    /**
     * 记录操作日志
     * @param log
     */
    @Transactional
    public void addLog(String log) {
        try {
            String accountId = SaTokenUtil.getUserId();

            String requestId = SaTokenUtil.getRequestId();

            LocalDateTime dateTime = LocalDateTime.now();

            String id = Util.getUUID32();

            AccessLog accessLog = new AccessLog();
            accessLog.setId(id);
            accessLog.setRequestId(requestId);
            accessLog.setLogContent(log);
            accessLog.setAccountId(accountId);
            accessLog.setCreateTime(dateTime);

            accessLogMapper.insert(accessLog);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
